<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>我的收藏</title>
    <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.js"></script>
    <link rel="stylesheet" type="text/css" href="css/css1.css"/>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }

        p {
            margin: 0;
        }

        a {
            text-decoration: none;
            color: #000;
        }

        h2 {
            margin: 0;
            padding: 0;
            font-weight: 300;
        }

        ol, ul {
            list-style: none;
            margin: 0;
            padding-left: 0;
        }

        .me-page {
            width: 990px;
            margin: 18px auto;
        }

        .home-navigation {
            width: 180px;
            float: left;
            margin-right: 8px;
            border: 1px solid #dedede;
        }

        .home-navigation p {
            height: 40px;
            line-height: 40px;
            padding: 0 8px;
            margin: 0;
            background-color: #e3e3e3;
            font-size: 20px;
            font-weight: 700;
        }

        .home-navigation p span {
            font-size: 9px;
            font-weight: 300;
            vertical-align: middle;
        }

        .user-thumb {
            padding: 20px 0;
        }

        .thumb-bg {
            width: 100px;
            height: 100px;
            margin: 0 auto;
            padding: 5px 7px 7px 5px;
        }

        .thumb-bg img {
            width: 100px;
            height: 100px;
        }

        .nav-title {
            height: 38px;
            line-height: 38px;
            font-size: 14px;
            border-top: 1px solid #ccc;
            color: black;
            background: #e6e6e6;
            margin: 0;
            padding-left: 0;
        }

        .title-icon {
            float: left;
            display: block;
            width: 15px;
            height: 15px;
            margin: 12px 10px 0;
            background: url("images/ucenter/nav-group.png");
        }

        .nav-group .nav-row.first {
            border-top: none;
        }

        .nav-group .nav-row {
            height: 38px;
            line-height: 38px;
            border-top: 1px solid #e2e2e2;
            padding-left: 35px;
            color: #666;
            background: #f8f8f8;
            font-size: 12px;
            cursor: pointer;
        }

        .nav-group .nav-row:hover {
            background-color: white;
        }

        .nav-group ul li.active.nav-row {
            background: #636363;
            color: #e3e3e3;
        }

        .me-main {
            float: left;
            color: black;
            width: 800px;
            box-sizing: border-box;
        }

        .latest-order {
            margin-bottom: 10px;
        }

        .latest-order .title {
            height: 40px;
            line-height: 40px;
            padding: 0 8px;
            background: #e3e3e3;
        }

        .latest-order .title > span {
            float: left;
            font-size: 16px;
            font-weight: 700;
        }

        .latest-order .title .more {
            float: right;
            display: block;
            height: 23px;
            line-height: 23px;
            width: 63px;
            margin-top: 8px;
            font-size: 10px;
            color: #fdfdfd;
            background-color: #636363;
            text-align: center;
            -webkit-border-radius: 6px;
            -moz-border-radius: 6px;
            border-radius: 6px;

        }

        .latest-order .swindle-info span {
            color: red;
        }

        .swindle-info {
            margin: 10px;
            background-color: #faf6da;
            color: black;
            font-size: 12px;
            padding: 10px;
        }

        .favorite-product {
            font-size: 12px;
        }

        .favorite-product .table-header {
            height: 30px;
            line-height: 30px;
            background: #efefef;
            border: 1px solid #e6e6e6;
            padding: 0 10px;
        }

        .table-header > span {
            display: block;
            float: left;
            text-align: center;
        }

        .table-header .info {
            width: 390px;
            text-align: left;
            padding-left: 50px;
        }

        .table-header .price {
            width: 200px;
            text-align: left;
        }

        .table-header .operation {
            width: 136px;
        }

        .empty-tip {
            padding: 200px 0;
            text-align: center;
            overflow: hidden;
        }

        .me-pager {
            width: 778px;
            height: 22px;
            line-height: 22px;
            background: #f3f3f3;
            border-top: 1px solid #e3e3e3;
            padding: 10px;
            font-size: 12px;
            color: #000;
        }

        .me-pager span:nth-child(1), .me-pager span:nth-child(2) {
            margin-left: 10px;
        }

        .favorite-product > ul {
            padding-bottom: 20px;

        }

        .fav-good {
            position: relative;
        }

        .fav-row {
            display: table;
            padding: 10px 0;
            border-bottom: 1px solid #e6e6e6;
        }

        .fav-row .checkbox {
            width: 40px;
            padding-left: 10px;
        }

        .fav-row .thumb {
            width: 110px;
            text-align: center;
            height: 60px;
        }

        .fav-row .thumb img {
            width: 100px;
            border: none;
        }

        .fav-row .text-info {
            width: 270px;
            line-height: 16px;
        }

        .fav-row .price {
            width: 200px;
            padding-left: 22px;
        }

        .fav-row .price > span {
            font-weight: 700;
            color: #e8044f;
            font-size: 14px;
        }

        .fav-row .options {
            width: 136px;
            text-align: center;
        }

        .fav-row .options > * {
            display: block;
        }

        .fav-row .options .buy-now {
            width: 92px;
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            text-align: center;
            margin: 0 auto;
            color: #fff;
            background: #da044a;
            border-radius: 4px;
        }

        .fav-row .options .del-favorite {
            margin: 5px auto;
            cursor: pointer;
            color: #aaa;
        }

        .fav-row > * {
            display: table-cell;
            vertical-align: middle;
        }
    </style>
</head>
<body>
<div id="app-userCenter">
    <div class="top">
        <div class="top-list">
            <div class="top-left" v-show="!isLogin">
                <a href="#"><span>雷猴~</span></a>
                <a href="login.html"><span>[请登录]</span></a>
                <a href="register.html"><span>[注册]</span></a>
            </div>
            <div class="top-right">
                <a href="user-center.html" v-show="isLogin"><span>{{ nickname }}</span></a>
                <a href="javascript:void(0)"><span style="cursor: pointer" @click="quitLogin"
                                                   v-show="isLogin">[退出]</span></a>
                <a href="user-order.html"><span>我的订单</span></a>
                <a href="#"><span>我的收藏</span></a>
            </div>
        </div>
    </div>
    <!--首页-->
    <div class="head-logo">
        <div class="head-inner">
            <!--logo-->
            <div class="header-logo">
                <a href="index.html"><img src="images/logo.png" alt="乐鲜生活" height="80"/></a>
            </div>
            <!--购物车-->
            <div class="buyCar">
                <a href="buy-cart.html">
                    <span class="shopping-bag-icon"></span>
                </a>
            </div>
            <div class="header-search">
                <!--搜索框-->
                <div class="search-form">
                    <input type="text" name="search-content" class="header-search-input" maxlength="100"
                           placeholder="搜一哈你想搜的内容"/>
                    <a href="#" class="header-search-button">
                        <span class="header-search-icon"></span>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <div class="header-nav">
        <div class="first-category">
            <ul class="first-category-ul">
                <li v-for="(item,index) in primaryCategory" @click="onPrimCateClick(index)" class="first-category-item">
                    <span>{{item.name}}</span>
                </li>

            </ul>

        </div>
    </div>
    <div class="user-module me-page">
        <div class="home-navigation">
            <!-- 标题 -->
            <a href="user-center.html"><p>个人中心<span>ucenter</span></p></a>
            <!-- 头像 -->
            <div class="user-thumb">
                <div class="thumb-bg">
                    <img id="user-thumb" src="images/ucenter/1.gif"/>
                </div>
            </div>
            <div class="nav-group">
                <h2 class="nav-title"><span class="title-icon"></span>交易管理</h2>
                <ul>
                    <li class="nav-row first"><a href="user-order.html">我的订单</a></li>
                    <li class="nav-row active">我的收藏</li>
                    <li class="nav-row">浏览记录</li>
                    <li class="nav-row">我的vip</li>
                </ul>
            </div>
            <div class="nav-group">
                <h2 class="nav-title"><span class="title-icon"></span>个人信息管理</h2>
                <ul>
                    <li class="nav-row first"><a href="user-message.html">编辑个人资料</a></li>
                    <li class="nav-row"><a href="user-address.html">地址管理</a></li>
                </ul>
            </div>
        </div>
        <div class="me-main">
            <div class="latest-order">
                <h2 class="title">
                    <span>最近订单</span>
                </h2>
                <div class="swindle-info">
                    <span>重要提醒：</span>近期假冒客服诈骗频发，leGo只会给您二维码引导您转账，谢谢配合。
                </div>
                <div class="favorite-product">
                    <p class="table-header">
                        <span class="info">商品信息</span>
                        <span class="price">单价（元）</span>
                        <span class="operation">操作</span>
                    </p>
                    <ul id="fav-good-ul">
                    </ul>
                    <p class="empty-tip hide">亲亲~你没有任何收藏哦！</p>
                </div>
                <div class="me-pager">
                    <a href="javascript:void(0)"><span class="pull-left collect-chooseAll">全选</span></a>
                    <a href="javascript:void(0)"><span class="pull-left collect-delete">删除</span></a>

                    <div class="pull-right">
                        <span>共<strong id="collect-count">0</strong>条结果，0/0条结果</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="./js/userCenter.js"></script>

<script>
    window.onload = checkUser;
    var result;
    var result1;
    var xmlHttp;
    var rowi;
    var userPhone;
    var resultLength;

    //      创建xmlHttp对象
    function createXMLHttpRequest() {
        if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
    }

    //      回调
    function callback() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            result = xmlHttp.responseText;
            analysisJSON();
        }
    }

    //      ajax传值
    function postValue() {
        createXMLHttpRequest();
        var post_str = "phone=" + window.localStorage.getItem("phone")
        var url = "http://120.77.223.88:8080/red/requestCollection";

        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=utf-8');
        xmlHttp.send(post_str);
        xmlHttp.onreadystatechange = callback;
    }

    //      检查是否登录
    function checkUser() {
//          如果本地cookie值返回false
        if((!(window.localStorage.getItem("isLogin") != undefined))|| window.localStorage.getItem("isLogin") != "true"){
            alert("请先登录");
            window.location.href = "login.html";
        } else {

            postValue();


        }
    }

    //得到数据然后生成收藏商品的li
    function analysisJSON() {
        result1 = eval('(' + result + ')').data;
        resultLength = result1.length;
        if (resultLength != 0) {
            createModule(resultLength);
        }
        else {
            $("ul#fav-good-ul").addClass("hide");
            $("p.empty-tip.hide").removeClass("hide");
        }

    }

    //      创建订单商品的div模块
    function createModule(resultLength) {
        var ul = document.getElementById("fav-good-ul");
        for (var i = 0; i < resultLength; i++) {
            rowi = result1[i];
            var li = document.createElement("li");
            li.classList.add("fav-good", "fav-row");
            var div1 = document.createElement("div");
            div1.classList.add("checkbox");
            var input = document.createElement("input");
            input.type = 'checkbox';
            div1.appendChild(input);
            li.appendChild(div1);
            var a1 = document.createElement("a");
            a1.classList.add("thumb");
            var img = document.createElement("img");
            img.src = rowi.image_main;
            img.width = '100';
            a1.appendChild(img);
            li.appendChild(a1);
            var div2 = document.createElement("div");
            div2.classList.add("text-info");
            div2.innerText = rowi.goods_name;
            li.appendChild(div2);
            var div3 = document.createElement("div");
            div3.classList.add("price");
            var span = document.createElement("span");
            span.innerText = "￥ " + rowi.price;
            div3.appendChild(span);
            li.appendChild(div3);
            var div4 = document.createElement("div");
            div4.classList.add("options");
            var a2 = document.createElement("a");
            a2.href = "javascript:void(0)";
            a2.classList.add("buy-now");
            a2.innerText = '立即购买';
            var a3 = document.createElement("a");
            a3.href = "javascript:void(0)";
            a3.classList.add("del-favorite");
            a3.innerText = '[ 删除 ]';
            div4.appendChild(a2);
            div4.appendChild(a3);
            li.appendChild(div4);

            ul.appendChild(li);
        }
    }

</script>
<script>
    //      全选订单
    var chooseAll = false;
    var checkBoxArr;
    $("span.pull-left.collect-chooseAll").click(function () {
        checkBoxArr = $("ul#fav-good-ul").find("input");
        for (var i = 0; i < checkBoxArr.length; i++) {
            checkBoxArr[i].checked = !chooseAll;
        }
        chooseAll = !chooseAll;
    });

    //      删除订单(点击事件)每次点击删除的时候都要调用一次显示所有订单
    var checkDelete;
    $("span.pull-left.collect-delete").click(function () {
//          得到所有的input标签;
        checkBoxArr = $("ul#fav-good-ul").find("input");

//          遍历如果有选中的就ajax发送信息到服务器进行删除，同时本地移除元素
        for (var i = 0; i < checkBoxArr.length; i++) {
            if (checkBoxArr[i].checked == true) {
                createXMLHttpRequest();
                var post_str = "goods_ID=" + result1[i].goods_ID + "&phone=" + window.localStorage.getItem("phone");
                var url = "http://120.77.223.88:8080/red/deleteGoodsInCollect";
                xmlHttp.open("POST", url, true);
                xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=utf-8');
                xmlHttp.send(post_str);
                xmlHttp.onreadystatechange = function () {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        console.log("删除成功");
                    }
                };
//                  移除元素
                $("li.fav-good.fav-row").eq(i).empty();
            }
            else {
            }
        }
//          判断如果现实列表为0，则提示收藏夹为空
//            console.log($("li.fav-good.fav-row").eq(0));
        for (var i = 0; i < checkBoxArr.length; i++) {
            if ($("li.fav-good.fav-row").eq(0).attr("offsetHeight") == 21) {
                $("li.fav-good.fav-row").eq(0).remove();
            }
        }
        if ($("li.fav-good.fav-row").length == 0) {
            $("ul#fav-good-ul").addClass("hide");
            $("p.empty-tip.hide").removeClass("hide");
        }

    });

</script>
</body>
</html>